package src.Main;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ComponentConverter {
    public static void convertFile(Path file) {
        try {
            String s = Files.readString(file);
            s = s.replace("Subtype=\"SteelPlate\"", "Subtype=\"StructuralMaterials\"")
                    .replace("Subtype=\"LargeTube\"", "Subtype=\"StructuralMaterials\"")
                    .replace("Subtype=\"SmallTube\"", "Subtype=\"StructuralMaterials\"")
                    .replace("Subtype=\"Construction\"", "Subtype=\"StructuralMaterials\"")
                    .replace("Subtype=\"InteriorPlate\"", "Subtype=\"StructuralMaterials\"")
                    .replace("Subtype=\"Girder\"", "Subtype=\"StructuralMaterials\"")
                    .replace("Subtype=\"Display\"", "Subtype=\"ElectricalMaterials\"")
                    .replace("Subtype=\"RadioCommunication\"", "Subtype=\"ElectricalMaterials\"")
                    .replace("Subtype=\"SolarCell\"", "Subtype=\"ElectricalMaterials\"")
                    .replace("Subtype=\"Medical\"", "Subtype=\"ElectricalMaterials\"")
                    .replace("Subtype=\"Detector\"", "Subtype=\"ElectricalMaterials\"")
                    .replace("Subtype=\"Thrust\"", "Subtype=\"BeamDevice\"")
                    .replace("Subtype=\"MetalGrid\"", "Subtype=\"SpecialSteel\"");


            Files.delete(file);
            Files.writeString(file, s, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void convertDirectory(String root) {
        List<Path> targets = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        try {
            Files.walkFileTree(Path.of(root), new FileVisitor<Path>() {
                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    convertFile(file);
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                    return FileVisitResult.CONTINUE;
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String root = "C:\\Users\\SWQXDBA\\AppData\\Roaming\\SpaceEngineers\\Mods\\新档core\\Data\\vanilla\\CubeBlocks";
        convertDirectory(root);
        convertFile(Path.of("C:\\Users\\SWQXDBA\\AppData\\Roaming\\SpaceEngineers\\Mods\\新档core\\Data\\weapon\\WeaponCore - Replace Vanilla Weapons\\CubeBlocks_Weapons.sbc"));

    }

}
